package handler

import (
	"common"
	"common/biz"
	"common/logs"
	"context"
	"core/repo"
	"core/service"
	"encoding/json"
	"framework/remote"
	"hall/models/request"
	"strconv"
)

type GameHandler struct {
	gameService      *service.GameService
	warehouseService *service.WarehouseService
	base             *BaseHandler
}

func (h *GameHandler) List(session *remote.Session, msg []byte) any {
	logs.Info("[Game] List msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[Game] List err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	ctx := context.TODO()
	data := h.gameService.GameList(ctx)
	return common.S(data)
}

func (h *GameHandler) Info(session *remote.Session, msg []byte) any {
	logs.Info("[Game] Info msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[Game] Info err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.CommonID
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[Game]用户%d Info err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	data, err := h.gameService.GameCardInfo(ctx, uint(uid), req.ID)
	if err != nil {
		return common.F(err)
	}
	return common.S(data)
}

func (h *GameHandler) MyRolePool(session *remote.Session, msg []byte) any {
	logs.Info("[Game] MyRolePool msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[Game] MyRolePool err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.CommonID
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[Game]用户%d MyRolePool err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	data := h.gameService.GameMyRolePool(ctx, uint(uid), req.ID)
	return common.S(data)
}

func (h *GameHandler) RolePool(session *remote.Session, msg []byte) any {
	logs.Info("[Game] MyRolePool msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[Game] MyRolePool err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.CommonID
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[Game]用户%d MyRolePool err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	data := h.gameService.GameRolePool(ctx, uint(uid), req.ID)
	return common.S(data)
}

func (h *GameHandler) BuyRole(session *remote.Session, msg []byte) any {
	logs.Info("[Game] BuyRole msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[Game] BuyRole err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.GameCardRole
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[Game]用户%d BuyRole err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	err := h.gameService.GameBuyRole(ctx, uint(uid), &req)
	if err != nil {
		return common.F(err)
	}
	return common.S(nil)
}

func (h *GameHandler) UseRole(session *remote.Session, msg []byte) any {
	logs.Info("[Game] UseRole msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[Game] UseRole err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.GameCardRole
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[Game]用户%d UseRole err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	ctx := context.TODO()
	err := h.gameService.GameUseRole(ctx, uint(uid), &req)
	if err != nil {
		return common.F(err)
	}
	return common.S(nil)
}

func (h *GameHandler) Exchange(session *remote.Session, msg []byte) any {
	logs.Info("[Game] Exchange msg:%v", string(msg))
	uid, _ := strconv.Atoi(session.GetUid())
	if uid <= 0 {
		logs.Error("[Game] Exchange err:未获取到UID")
		return common.F(biz.InvalidUsers)
	}
	if err := h.base.Login(nil, uint(uid), session.GetSid()); err != nil {
		return common.F(err)
	}
	var req request.GameExchange
	if err := json.Unmarshal(msg, &req); err != nil {
		logs.Error("[Game]用户%d Exchange err:缺少参数 req=%v,err=%v", uid, req, err)
		return common.F(biz.RequestDataError)
	}
	if req.Num <= 0 {
		logs.Error("[Game]用户%d Exchange err:缺少参数 req=%v", uid, req)
		return common.F(biz.ZeroError)
	}
	ctx := context.TODO()
	err := h.gameService.GameExchange(ctx, uint(uid), &req)
	if err != nil {
		return common.F(err)
	}
	return common.S(nil)
}

func NewGameHandler(r *repo.Manager) *GameHandler {
	return &GameHandler{
		gameService:      service.NewGameService(r),
		warehouseService: service.NewWarehouseService(r),
		base:             NewBaseHandler(r),
	}
}
